package com.css.fxfzysx.modules.vabStructure.utils;


import com.css.fxfzysx.base.exception.ParamException;
import com.css.fxfzysx.modules.vabStructure.entity.CztCrsCalRoofbeamResult;
import com.css.fxfzysx.modules.vabStructure.entity.CztCrsEntiretyCi;
import com.css.fxfzysx.modules.vabStructure.repository.*;
import com.css.fxfzysx.modules.vabStructure.repository.impl.CrsEntiretyCiRepository;
import com.css.fxfzysx.modules.ysxManagement.entity.CStructureCiEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.DynamicFieldsEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.RcStructureRepository;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.modules.ysxManagement.vo.CStructureCiVo;
import com.css.fxfzysx.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;

@Component
public class SrcAnaluseUtils  {
    @Autowired
    private RcStructureRepository rcStructureRepository;

    @Autowired
    private CrsEntiretyCalResultRepository crsEntiretyCalResultRepository;

    @Autowired
    private CrsEntiretyCiRepository crsEntiretyCiRepository;

    @Autowired
    private CrsMatrixCalResultRepository crsMatrixCalResultRepository ;

    @Autowired
    private CrsColumnCalResultRepository crsColumnCalResultRepository ;

    @Autowired
    private ZqtCiRepository zqtCiRepository ;

    @Autowired
    private CrsRoofbeamCalRepository roofbeamCalRepository;

    @Autowired
    private CztCrsCalJlqResultRepository jlqResultRepository;

    @Autowired
    private CztCrsCalTcqResultRepository tcqResultRepository ;

    @Autowired
    private CrsStoreyCalResultRespository storeyCalResultRespository;

    public CStructureCiVo  getAllCStructureCi(String titleName,String calId){
        CStructureCiVo cStructureCiVo = new CStructureCiVo();
        List<CStructureCiEntity> cStructureCiList = rcStructureRepository.getCStructureCi();

        List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);

        List<CztCrsEntiretyCi> entiretyCis = crsEntiretyCiRepository.findByEntiretyId(calId);
        cStructureCiVo.setStructureCiList(cStructureCiList);
        cStructureCiVo.setDynamicFieldsEntityList(dynamicFields);
        cStructureCiVo.setEntiretyCiList(entiretyCis);
        return cStructureCiVo;
    }
    //梁参数校验
    public static void  beamParamVerification(List<CztCrsCalRoofbeamResult> beamVOS, String type){
        if(PlatformObjectUtils.isEmpty(beamVOS)){
            throw  new ParamException("梁参数不能为空，请检查后重新输入");
        }
        for (CztCrsCalRoofbeamResult beam:beamVOS) {

           /* if(type.equals("边柱")&&(beam.getBeamNum().equals("i3")||beam.getBeamNum().equals("i4"))){
                continue;
            }*/
            if(PlatformObjectUtils.isEmpty(beam.getBeamSectionHeight())){
                throw new ParamException(beam.getBeamNum()+"的截面高不能为空");
            }else if(PlatformObjectUtils.isEmpty(beam.getBeamSectionWidth())){
                throw new ParamException(beam.getBeamNum()+"的截面宽不能为空");
            }
        }
    }
    @Transactional
    public  void deleteByEntiretyId(String entiretyId){
        crsColumnCalResultRepository.deleteByEntiretyId(entiretyId);
        crsEntiretyCalResultRepository.deleteByCalId(entiretyId);
        crsMatrixCalResultRepository.deleteByEntiretyId(entiretyId);
        roofbeamCalRepository.deleteByEntiretyId(entiretyId);
        jlqResultRepository.deleteByEntiretyId(entiretyId);
        tcqResultRepository.deleteByEntiretyId(entiretyId);
        storeyCalResultRespository.deleteByEntiretyId(entiretyId);

    }

    public String getNumnerFromDatabase(){
        Integer number = crsEntiretyCalResultRepository.findMaxNum();
        if(PlatformObjectUtils.isNotEmpty(number)){
            number = number+1;
        }else{
            number = 1 ;
        }
        String str = String.valueOf(number);
        StringBuffer sb = new StringBuffer();

        for (int i = str.length(); i < 3 ; i++) {
            sb = sb.append("0");
        }

        str = sb.append(str).toString();
        return str ;
    }

}
